//
//  XGL_MessageConversationTableViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/27.
//

import UIKit

class XGL_MessageConversationTableViewCell: UITableViewCell {
    
    var itemHeaderView: UIImageView!
    var itemNumberText: UILabel!
    var itemNameText: UILabel!
    var itemContentText: UILabel!
    var itemDateText: UILabel!
    
    var itemData: XGL_MessageChatData = XGL_MessageChatData(){
        didSet{
            self.itemHeaderView.loadImageWithUrl(itemData.header)
            itemNumberText.isHidden = true
            itemNameText.text = itemData.name
            if itemData.messageType == 0{
                itemContentText.text = itemData.content
            }else if itemData.messageType == 1{
                itemContentText.text = "You sent a picture"
            }else if itemData.messageType == 2{
                itemContentText.text = "You sent a video"
            }else if itemData.messageType == 3{
                itemContentText.text = "You gave a gift"
            }
            if let date = itemData.time.stringToDate() {
                if date.isToday{
                    self.itemDateText.text = date.toFormat("HH:mm")
                }else{
                    self.itemDateText.text = date.toFormat("MM dd")
                }
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func createXglView(){
        let cardContentView = UIView().then { make in
            make.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
            make.layerCornerRadius = 16.0
        }
        self.itemHeaderView = UIImageView().then({ make in
            make.layerCornerRadius = 21.0
            make.clipsToBounds = true
        })
        self.itemNumberText = UILabel().then({ make in
            make.textColor = UIColor.hexColor("#191A1D")
            make.font = UIFont.pingfangSC_Semibold(fontSize: 12.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 8.0
            make.isHidden = true
        })
        self.itemNameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
        })
        self.itemContentText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        })
        self.itemDateText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        })
        
        cardContentView.addSubview(self.itemHeaderView)
        cardContentView.addSubview(self.itemNumberText)
        cardContentView.addSubview(self.itemNameText)
        cardContentView.addSubview(self.itemContentText)
        cardContentView.addSubview(self.itemDateText)
        self.contentView.addSubview(cardContentView)
        
        itemHeaderView.snp.makeConstraints { make in
            make.left.top.equalTo(9.0)
            make.width.height.equalTo(42.0)
        }
        itemNumberText.snp.makeConstraints { make in
            make.right.equalTo(itemHeaderView.snp.right).offset(1.0)
            make.top.equalTo(itemHeaderView.snp.top).offset(-1.0)
            make.height.equalTo(8.0)
            make.width.equalTo(8.0)
        }
        self.itemNameText.snp.makeConstraints { make in
            make.left.equalTo(itemHeaderView.snp.right).offset(17.0)
            make.top.equalTo(9.0)
        }
        itemContentText.snp.makeConstraints { make in
            make.left.equalTo(itemNameText)
            make.bottom.equalTo(itemHeaderView)
            make.right.equalTo(-16.0)
        }
        itemDateText.snp.makeConstraints { make in
            make.centerY.equalTo(itemNameText)
            make.right.equalTo(-16.0)
        }
        cardContentView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.top.equalTo(11.0)
            make.centerY.equalToSuperview()
        }
    }
}
